<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>Prevent default example</title>
    <style>
      div {
        margin-bottom: 10px;
      }
    </style>
  </head>
  <body>
    <form>
      <div>
        <label for="fname">First name: </label>
        <input id="fname" type="text">
      </div>
      <div>
        <label for="lname">Last name: </label>
        <input id="lname" type="text">
      </div>
      <div>
        <input id="submit" type="submit">
      </div>
    </form>
    <p></p>
    <script>
      const form = document.querySelector('form');
      const fname = document.getElementById('fname');
      const lname = document.getElementById('lname');
      const para = document.querySelector('p');

      form.addEventListener('submit', e => {
        if (fname.value === '' || lname.value === '') {
          e.preventDefault();
          para.textContent = 'You need to fill in both names!';
        }
      });
    </script>
  </body>
</html>
